package cn.exrick.xboot.quartz.jobs;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.exception.XbootException;
import cn.exrick.xboot.modules.base.entity.QuartzJob;
import cn.exrick.xboot.modules.base.service.QuartzJobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *系统监控的计划任务（任务调度）
 * @Auther: njp
 * @Date: 2020/10/19/10:10
 * @Description:
 */
public class SystemCheckJob implements Job {

    @Autowired
    private QuartzJobService quartzJobService;
    @Autowired
    private Scheduler scheduler;
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //1.调用监控业务 todo
        //2.
        try {
                this.stopJob();
        } catch(Exception e){
            this.stopJob();
            e.printStackTrace();
        }
    }

    public void stopJob(){
        List<QuartzJob> list = quartzJobService.findByJobClassName(this.parameter);
        try{
            if(list!=null&&list.size()>0){
                QuartzJob  job=list.get(0);
                scheduler.pauseJob(JobKey.jobKey(job.getJobClassName()));
                job.setStatus(CommonConstant.STATUS_DISABLE);
                quartzJobService.update(job);
            }
        } catch (SchedulerException e) {
            throw new XbootException("暂停定时任务失败");
        }
    }
}
